/*
 * @(#)FullTextExtractorTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.taglib;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationContainer;
import com.dcivision.framework.FullTextIndexFactory;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;

/**
 * <p>Class Name:    FullTextExtractorTag.java    </p>
 * <p>Description:   This tag is used to extract the full text of the document
 * @author           Roy Wong
 * @company          DCIVision Limited
 * @creation date    06/04/2005
 * @version          $Revision: 1.2.2.1 $
 */

public class FullTextExtractorTag extends BodyTagSupport {
 
  public static final String REVISION = "$Revision: 1.2.2.1 $"; 
  
  private static final Log log = LogFactory.getLog(FullTextExtractorTag.class);
  
  private DmsDocument docObject = null;
  private Integer length = null;
  private String sPermission = null;
  
  public DmsDocument getDocObject() {
    return this.docObject;
  }
  
  public void setDocObject(DmsDocument docObject) {
    this.docObject = docObject;
  }
  
  public Integer getLength(){
    return this.length;
  }
  
  public void setLength(Integer length){
    this.length = length;
  }
  
  public void setPermission(String permission) {
    this.sPermission = permission;
  }

  public String getPermission(){
    return this.sPermission;
  }
  
  public int doStartTag() throws JspException {

     try {
       pageContext.getOut().print(this.src());
       return(SKIP_BODY);
     } catch (Exception e) {
       log.error(e, e);
       throw new JspException(e);
     }
   }


   public int doEndTag() throws JspException {
     return EVAL_PAGE;
   }
  
  protected java.lang.StringBuffer src() throws javax.servlet.jsp.JspException {
    StringBuffer buffer = new StringBuffer();
    HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    ApplicationContainer applicationContainer = (ApplicationContainer)request.getSession().getServletContext().getAttribute(GlobalConstant.APPLICATION_CONTAINER_KEY);
    Connection conn = (Connection) pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    Query query = null;
    Hits hits = null;
    DmsDocument dmsDocument = this.docObject;
    String content = "";
    Analyzer analyzer = null; 
    try{
    
      analyzer = new org.apache.lucene.analysis.WhitespaceAnalyzer();
      QueryParser qParser = new QueryParser("FULL_TEXT", analyzer);
      query = qParser.parse("DCI_OBJ_TYPE:\"D\" && DCI_RECORD_STATUS:\""+(GlobalConstant.RECORD_STATUS_ACTIVE+GlobalConstant.RECORD_STATUS_ACTIVE).toLowerCase()+"\" && DCI_ID:\"" + dmsDocument.getID() + "\"");
      hits = FullTextIndexFactory.getInstance().queryHits(query, sessionContainer,null,null); 
      if(hits.length() > 0){
        Document doc = hits.doc(0);      
        content = TextUtility.noNull(doc.get("FULL_TEXT"));
      }
    
      if((!Utility.isEmpty(sPermission)) && (sPermission.indexOf("R")>=0)){
      
        if((!Utility.isEmpty(content)) && (!Utility.isEmpty(this.length)) && (this.length.intValue()>0)){
           if(content.length()>this.length.intValue()){
             buffer.append(content.substring(0,this.length.intValue()));   
           }else{
             buffer.append(content);
           }  
        }else if((!Utility.isEmpty(content)) && (Utility.isEmpty(this.length))){
          buffer.append(content);
        }else{
          buffer.append("");   
        }
      }else{
        buffer.append(""); 
      }
    }catch(Exception e){
      log.error(e, e);
      throw new JspException(e);
    }finally{
      docRetrievalManager.release();
      conn = null;
      query = null;
      hits = null;
      analyzer=null;     
    }
    return buffer;
  }
  
  public void release() {
    this.docObject = null;
    this.length = null;
    super.release();
  }
  
}